package com.mhkj.revise.mainPlan.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.templete.service.PlanTempleteService;
import com.mhkj.common.enums.PlanType;
import com.mhkj.revise.mainPlan.service.MainPlanNodeService;
import com.mhkj.revise.mainPlan.service.MainPlanService;
import com.mhkj.revise.plan.entity.Plan;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.common.exception.ParamException;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 主项计划
 * @author mics
 * @date 2018年8月17日
 * @version 1.0
 */
@Controller
@RequestMapping("/mainPlan")
public class MainPlanController extends BaseController<Plan>{
	@Resource
	private MainPlanService mainPlanService;
	@Resource
	private JsonHandler jsonHandler;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private PlanTempleteService planTempleteService;
	@Resource
	private MainPlanNodeService mainPlanNodeService;
	
	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response,Plan plan,int pageIndex,int pageSize){
		Pagination<Plan> pagination =  mainPlanService.listByPage(plan,pageIndex,pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination, new String[]{
			"id","name","item.name","plan.name","beginDate","endDate","createDate","state","auditDate","auditRemark"}, 
			"id","name","itemName","keyPlanName","beginDate","endDate","createDate","state","auditDate","auditRemark");
	}
	
	
	/**
	 * 保存主项计划
	 * @author mics
	 * @date 2018年8月18日
	 * @version 1.0
	 */
	@RequestMapping("/save")
	@ResponseBody
	public void save(HttpServletResponse response,Plan plan){
		if(plan == null){
			throw new ParamException(ErrorCode.PARAM_ERROR,"参数错误！");
		}
		
		//保存计划类型
		Dictionary planType = dictionaryService.getByCode(PlanType.MAIN_TYPE.getCode());
		plan.setPlanType(planType);
		
		//保存主项计划
		String message =  mainPlanService.save(plan);
		if(!"ok".equals(message)){
			jsonHandler.sendJsonSimple(response, false, message);
		}else{
		    plan = mainPlanService.getById(plan.getId());
			jsonHandler.sendJsonString(response, true, "获取数据成功", plan, new String[]{"id"}, "planId");
		}	
	}
	
	/**
	 * 推算周期
	 * @author mics
	 * @date 2018年8月13日
	 * @version 1.0
	 */
	@RequestMapping("/calsDealine")
	@ResponseBody
	public ResponseMessage calsDealine(HttpServletResponse response,long planId){
		mainPlanNodeService.calsDealinePrepare(planId);
		return ResponseMessage.createMessage(true, "推算周期成功");
	}
	
	
	/**
	 * 查询所有关键计划
	 * @author mics
	 * @date 2018年8月20日
	 * @version 1.0
	 */
	@RequestMapping("/list")
	@ResponseBody
	public List<Plan> listAll(){
		List<Plan> plans = mainPlanService.findMainPlan();
		List<Plan> planTemps = new ArrayList<Plan>();
		for(Plan plan :plans){
			Plan planTemp = new Plan();
			planTemp.setName(plan.getName());
			planTemp.setId(plan.getId());
			planTemps.add(planTemp);
		}
		return planTemps;
	}


}
